package com.itheima.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Keyword;
import com.itheima.service.KeywordService;
import com.itheima.service.impl.KeywrodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author stay foolish
 * @description 行至水穷处, 坐看云起时
 * @date 2022/11/18 20:51:02
 * @description TODD
 */
@WebServlet("/keywordServlet")
public class KeywordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.处理请求乱码
        request.setCharacterEncoding("utf-8");
        // 2.获取请求参数
//        String id = request.getParameter("id");
//        int id1 = Integer.parseInt(id);
        String keyword = request.getParameter("name");
        // 3.创建keyword实体类对象
//        Keyword keyword = new Keyword();
//        keyword.setId(id1);
//        keyword.setName(name);
        // 4.创建service实现类对象
        KeywrodServiceImpl keywrodService = new KeywrodServiceImpl();
        // 5.调用方法
        List<Keyword> keywords = keywrodService.find(keyword);
        // 6.设置响应乱码
        response.setContentType("text/html;charset=utf-8");
        // 7.将其转化为json字符串
        String str = JSON.toJSONString(keywords);
        // 8.将json字符串响应给浏览器
        response.getWriter().write(str);
    }
}

